<?php

	/**
	 * Crawls wine item from winelog.com
	 *
	 * @author anton.perkin
	 * @version 1.0
	 *
	 */
	class WineLog implements WineSource
	{
		/* VARIABLES */
		
		/**
		 * Wine id
		 *
		 * @var int
		 */
//		public $id;
		
		/**
		 * Wine list base URL
		 *
		 * @var string
		 */
		public $listUrl = 'http://www.winelog.net/wines';
		
		/**
		 * Wine item base URL
		 *
		 * @var string
		 */
		public $itemUrl = 'http://www.winelog.net/wines/wine/';
		
		/**
		 * Wine URLs
		 *
		 * @var array
		 */
		public $wineURLs;
		
		
		/* CONSTRUCTORS */
			
		/**
		 * Defualt constructor
		 *
		 */
		public function WineLog()
		{
//			$this->id = $id;
		}		
		
		/* METHODS */
		
		/**
		 * Gets list of wine URLs
		 *
		 * @param int $fromPageNumber from page
		 * @param int $toPageNumber to page
		 * 
		 */
		public function GetWineUrls($fromPageNumber, $toPageNumber)
		{
			$wines = array();
			
			for ($i = $fromPageNumber; $i <= $toPageNumber; $i++)
			{
				//parsed page
				$page = phpQuery::newDocumentFileHTML($this->listUrl."?page=".$i);
				
				$results = $page->find(".search_result > a:first-child");
				foreach ($results as $result)
				{
					$this->wineURLs[] = pq($result)->attr('href');
				}
				
			}
		}
		
		/**
		 * Saves wine URLs
		 * 
		 * @return boolean
		 *
		 */
		public function SaveWineURLs()
		{
			$result = array('success' => 0, 'fail' => 0);
			
			foreach ($this->wineURLs as $url)
			{
				$wineItem = new WineItem();
				$saveResult = $wineItem->SaveURL($url, 'winelog');
				if ($saveResult)
				{
					$result['success']++;
				}
				else 
				{
					$result['fail']++;
				}
			}
			
			return $result;
		}
		
		/**
		 * Gets wine URLs list
		 *
		 * @param int $fromRecord offset
		 * @param int $toRecord count
		 */
		public function GetSavedWineURLs($fromRecord = '', $toRecord = '')
		{
			//limit
			if (($fromRecord == '') && ($toRecord == ''))
			{
				$limit = '';
			}
			else 
			{
				$fromRecord = ($fromRecord == '') ? 0 : $fromRecord;
				$toRecord = ($toRecord == '') ? 1000000 : $toRecord;
				$limit = "LIMIT $fromRecord, $toRecord";
			}
			
			$urls = DB::ListItems("
				SELECT * FROM t_wine_urls WHERE source = 'winelog' $limit;
			");
			
			return $urls;
		}
		
		/**
		 * Gets wine URLs list for empty wines
		 *
		 */
		public function GetEmptyWineURLs()
		{
			$emptyWines = DB::ListItems("SELECT t_wines.url_id FROM t_wines WHERE t_wines.name IS NULL OR t_wines.name = ''");
			foreach ($emptyWines as $emptyWine)
			{
				$emptyURL = DB::ListItems("SELECT * FROM t_wine_urls WHERE t_wine_urls.source = 'winelog' AND t_wine_urls.id = ".$emptyWine['url_id']);
				$emptyURLs[] = $emptyURL[0];
			}
			
			return $emptyURLs;
		}		
		
		/**
		 * Gets wine item
		 * 
		 * @param int $id wine id
		 * 
		 * @return WineItem
		 *
		 */
		public function GetWineItem($url)
		{
			//parsed page
//			$ch = curl_init();
//			curl_setopt($ch, CURLOPT_URL, $url);
//			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//			$contents = curl_exec($ch);
//			curl_close ($ch);			
//			$page = phpQuery::newDocumentHTML($contents);
			$page = phpQuery::newDocumentFileHTML($url);
			
			//wine block
			$wineBlock = $page->find("#left2_panel");

			//wine info
			$wineInfo = pq($wineBlock)->find("#wine_info > ul");
			
			//wine item
			$wineItem = new WineItem();
			
			//id
			$urlElements = explode('/', $url);
			$wineItem->id = $urlElements[count($urlElements) - 2];
			
			//display name
			$wineItem->displayName = trim(pq($wineBlock)->find("#heading_tan .fn")->html());
			
			//year
			$wineItem->vintage = intval(substr($wineItem->displayName, -4));
		
			//name
			if (empty($wineItem->vintage))
			{
				$wineItem->vintage = 'NULL';
				$wineItem->name = $wineItem->displayName;
			}
			else 
			{
				$wineItem->name = trim(str_replace($wineItem->vintage, '', $wineItem->displayName));
			}	
			
			//wine info
			$wineInfoElements = array('region', 'winery', 'type', 'variety', '', '', '', '');
			foreach ($wineInfo->children() as $key => $infoItem)
			{
				switch ($wineInfoElements[$key])
				{
					//TODO: region/subregion division
					case 'region':
						$wineItem->region = pq($infoItem)->find("a")->html();
						$wineItem->subRegion = 'NULL';
						break;
						
					case 'winery':
						$wineItem->producer = pq($infoItem)->find("a")->html();
						break;
						
					case 'type':
						$wineItem->type = pq($infoItem)->find("a")->html();
						break;
						
					case 'variety':
						$wineItem->varietal = pq($infoItem)->find("a")->html();
						break;
						
				}
			}
			
			//item url
			$wineItem->webAddress = $url;
			
			//image url
			$wineItem->photoURL = pq($wineInfo)->find("#wine_pic img")->attr('src');
			
			//source
			$wineItem->source = 'winelog';
			
			return $wineItem;
		}
	}

?>